package gui;

/*
 * The main window class
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import model.ChatMsgs;

/**
 *
 * @author Nana
 */
public class MainWindow extends JFrame {

    private JPanel jpanTop;
    private JPanel jpanMiddle;
    private JPanel jpanBottom;
    private JPanel jpanBottomLeft;
    private JScrollPane jspChatMsgs;
    private JScrollPane jspConnectedUsers;
    private JButton jbutConnect;
    private JButton jbutDisconnect;
    private JButton jbutChooseFile;
    private JButton jbutSend;
    private JTextField jtfUsername;
    private ChatPane jepChatMsgs;
    private JTextArea jtaMsgToSend;
    private JList jlstConnectedUsers;
    private DefaultListModel connectedUsers;
    private boolean connected = false;
    private String username;
    private ChatGUI gui;
    private BufferedImage bImgConnect;
    private BufferedImage bImgDisconnect;
    private BufferedImage bImgSend;
    private BufferedImage bChooseFile;
    private BufferedImage bImgPressedConnect;
    private BufferedImage bImgPressedDisconnect;
    private BufferedImage bImgDisabledConnect;
    private BufferedImage bImgPressedSend;
    private BufferedImage bImgChoosefile;
    private BufferedImage bImgDisabledSend;
    private BufferedImage bImgPressedChoosefile;
    private BufferedImage bImgDisabledChoosefile;
    private BufferedImage bImgDisabledDisconnect;
    private Object scrollPane;

    /**
     * The constructor of the Main Window
     *
     * @param gui The chat gui
     * @param userList The list of connected users
     * @param chatmsgs The field of chat messages
     * @throws IOException
     */
    public MainWindow(ChatGUI gui, DefaultListModel userList, ChatMsgs chatmsgs) throws IOException {
        super("ChatSystem_UI_V1.0");
        this.gui = gui;
        this.connectedUsers = userList;

        this.setSize(800, 600);
        this.setResizable(false);
        this.setLocationRelativeTo(null);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new BorderLayout());

        this.jpanTop = new JPanel();
        this.jpanMiddle = new JPanel();
        this.jpanBottom = new JPanel();
        this.jpanBottomLeft = new JPanel();

        jpanTop.setPreferredSize(new Dimension(800, 60));
        jpanMiddle.setPreferredSize(new Dimension(800, 400));
        jpanBottom.setPreferredSize(new Dimension(800, 140));
        jpanBottomLeft.setPreferredSize(new Dimension(150, 110));

        jpanTop.setBackground(new Color(112, 19, 87));
        jpanMiddle.setBackground(new Color(112, 19, 87));
        jpanBottom.setBackground(new Color(112, 19, 87));
        jpanBottomLeft.setBackground(new Color(112, 19, 87));
        JPanel jpanVoid;
        JPanel jpanVoidBottom;

        /*
         * TOP PANEL
         */
        this.jtfUsername = new JTextField();
        this.jtfUsername.setPreferredSize(new Dimension(130, 30));
        this.jtfUsername.setText("Poney");
        this.jtfUsername.setBackground(new Color(231, 207, 225));
        this.jtfUsername.setCaretColor(Color.pink);

        this.jbutConnect = new JButton("Start Galloping");
        this.jbutConnect.setPreferredSize(new Dimension(150, 50));
        this.jbutConnect.setBorderPainted(false);

        this.jbutDisconnect = new JButton("Fly away");
        this.jbutDisconnect.setPreferredSize(new Dimension(150, 50));
        this.jbutDisconnect.setBorderPainted(false);

        jpanVoid = new JPanel();
        jpanVoid.setBackground(new Color(112, 19, 87));
        JLabel jpanvoidimg = new JLabel(new ImageIcon(ImageIO.read(this.getClass().getResourceAsStream("/Images/jpanvoid.jpg"))));
        jpanVoid.setPreferredSize(new Dimension(300, 50));
        jpanVoid.add(jpanvoidimg);

        try {
            this.bImgConnect = ImageIO.read(this.getClass().getResourceAsStream("/Images/bconnect.jpg"));
            this.bImgPressedConnect = ImageIO.read(this.getClass().getResourceAsStream("/Images/bconnectActv.jpg"));
            this.bImgDisabledConnect = ImageIO.read(this.getClass().getResourceAsStream("/Images/bconnectOff.jpg"));
            this.bImgDisconnect = ImageIO.read(this.getClass().getResourceAsStream("/Images/bdisconnect.jpg"));
            this.bImgPressedDisconnect = ImageIO.read(this.getClass().getResourceAsStream("/Images/bdisconnectActv.jpg"));
            this.bImgDisabledDisconnect = ImageIO.read(this.getClass().getResourceAsStream("/Images/bdisconnectOFF.jpg"));
        } catch (IOException e) {
            this.bImgConnect = null;
            this.bImgPressedConnect = null;
            this.bImgDisabledConnect = null;
            this.bImgDisconnect = null;
            this.bImgPressedDisconnect = null;
            this.bImgDisabledDisconnect = null;
            System.out.println("Invalid Graphic File");
        }

        this.jbutConnect.setIcon(new ImageIcon(bImgConnect));
        this.jbutConnect.setBackground(new Color(112, 19, 87));
        this.jbutConnect.setPressedIcon(new ImageIcon(bImgPressedConnect));
        this.jbutConnect.setDisabledIcon(new ImageIcon(bImgDisabledConnect));

        this.jbutDisconnect.setIcon(new ImageIcon(bImgDisconnect));
        this.jbutDisconnect.setPressedIcon(new ImageIcon(bImgPressedDisconnect));
        this.jbutDisconnect.setDisabledIcon(new ImageIcon(bImgDisabledDisconnect));
        this.jbutDisconnect.setBackground(new Color(112, 19, 87));

        this.jpanTop.add(this.jtfUsername);
        this.jpanTop.add(this.jbutConnect);
        this.jpanTop.add(jpanVoid);
        this.jpanTop.add(this.jbutDisconnect);

        /*
         * MIDDLE PANEL
         */
        this.jepChatMsgs = new ChatPane();
        chatmsgs.addObserver(this.jepChatMsgs);
        this.jepChatMsgs.setEditable(false);
        this.jepChatMsgs.setContentType("text/html");
        this.jspChatMsgs = new JScrollPane(this.jepChatMsgs);
        this.jspChatMsgs.getVerticalScrollBar().addAdjustmentListener(new AdjustmentListener() {
            public void adjustmentValueChanged(AdjustmentEvent e) {
                e.getAdjustable().setValue(e.getAdjustable().getMaximum());
            }
        });
        this.jspChatMsgs.setPreferredSize(new Dimension(600, 355));
        this.jepChatMsgs.setBackground(new Color(248, 246, 247));
        this.jspChatMsgs.setBackground(new Color(248, 246, 247));

        this.jlstConnectedUsers = new JList(this.connectedUsers);
        this.jlstConnectedUsers.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        this.jspConnectedUsers = new JScrollPane(this.jlstConnectedUsers);
        this.jspConnectedUsers.setPreferredSize(new Dimension(160, 355));
        this.jspConnectedUsers.setBackground(new Color(248, 246, 247));
        this.jlstConnectedUsers.setBackground(new Color(248, 246, 247));

        this.jpanMiddle.add(this.jspChatMsgs);
        this.jpanMiddle.add(this.jspConnectedUsers);

        /*
         * BOTTOM PANEL
         */
        this.jtaMsgToSend = new JTextArea("Enter your message...");
        this.jtaMsgToSend.setPreferredSize(new Dimension(600, 125));
        this.jtaMsgToSend.setBackground(new Color(231, 207, 225));

        this.jbutChooseFile = new JButton("Choose a saddle");
        this.jbutChooseFile.setPreferredSize(new Dimension(120, 50));

        this.jbutSend = new JButton("Whinny");
        this.jbutSend.setPreferredSize(new Dimension(120, 50));

        try {
            this.bImgSend = ImageIO.read(this.getClass().getResourceAsStream("/Images/bsend.jpg"));
            this.bImgPressedSend = ImageIO.read(this.getClass().getResourceAsStream("/Images/bsendActv.jpg"));
            this.bImgDisabledSend = ImageIO.read(this.getClass().getResourceAsStream("/Images/bsendOff.jpg"));
            this.bImgChoosefile = ImageIO.read(this.getClass().getResourceAsStream("/Images/bchoosefile.jpg"));
            this.bImgPressedChoosefile = ImageIO.read(this.getClass().getResourceAsStream("/Images/bchoosefileActv.jpg"));
            this.bImgDisabledChoosefile = ImageIO.read(this.getClass().getResourceAsStream("/Images/bchoosefileOff.jpg"));
        } catch (IOException e) {
            this.bImgSend = null;
            this.bImgPressedSend = null;
            this.bImgDisabledSend = null;
            this.bImgChoosefile = null;
            this.bImgPressedChoosefile = null;
            this.bImgDisabledChoosefile = null;
            System.out.println("Invalid Graphic File");
        }

        this.jbutSend.setIcon(new ImageIcon(bImgSend));
        this.jbutSend.setBackground(new Color(112, 19, 87));
        this.jbutSend.setPressedIcon(new ImageIcon(bImgPressedSend));
        this.jbutSend.setDisabledIcon(new ImageIcon(bImgDisabledSend));
        this.jbutSend.setBorderPainted(false);

        this.jbutChooseFile.setIcon(new ImageIcon(bImgChoosefile));
        this.jbutChooseFile.setBackground(new Color(112, 19, 87));
        this.jbutChooseFile.setPressedIcon(new ImageIcon(bImgPressedChoosefile));
        this.jbutChooseFile.setDisabledIcon(new ImageIcon(bImgDisabledChoosefile));
        this.jbutChooseFile.setBorderPainted(false);

        jpanBottomLeft.add(this.jbutSend);
        jpanBottomLeft.add(this.jbutChooseFile);

        this.jpanBottom.add(this.jpanBottomLeft);
        this.jpanBottom.add(this.jtaMsgToSend);
        this.add(jpanTop, BorderLayout.NORTH);
        this.add(jpanMiddle, BorderLayout.CENTER);
        this.add(jpanBottom, BorderLayout.SOUTH);

        this.setVisible(true);

        this.jepChatMsgs.setText("<html><h1><font color=\"#8d1249\"> Welcome to the Unicorn Chat </font></h1><p></p></html>");

        jbutDisconnect.addActionListener(new DisconnectListener(jtfUsername, gui, jlstConnectedUsers, jbutDisconnect, jbutConnect, jbutSend, jbutChooseFile));
        jbutChooseFile.addActionListener(new ChooseFileListener(gui, jlstConnectedUsers));
        jbutSend.addActionListener(new SendListener(jtaMsgToSend, gui, jlstConnectedUsers));
        jbutDisconnect.setEnabled(false);
        jbutChooseFile.setEnabled(false);
        jbutSend.setEnabled(false);
        jbutConnect.addActionListener(new ConnectListener(jtfUsername, gui, jbutConnect, jbutDisconnect, jbutSend, jbutChooseFile));

    }

}
